<?php 

function printTable($table, $swaps = null){

	$nbRows = mysql_num_rows($table);
	$row = mysql_fetch_array($table, MYSQL_ASSOC);
	if(!$row) echo("Nothing to display");
	else{
	$keys = array_keys($row);
	
	echo ('<table border="2">');
	
	if($nbRows == 1){		// PRINTS A VERTICAL TABLE IF THERE IS ONLY ONE ROW
		for($i = 0; $i < count($keys); $i++){
			$result = $row[$keys[$i]];
			$find = array("KEYMARKER", "VALUEMARKER", "INITMARKER");
			$replace = array($keys[$i], $row[$keys[$i]], $row[$keys[0]]);
			
			if(isset($swaps[$keys[$i]]))
				$result = str_replace($find, $replace, $swaps[$keys[$i]]);
			elseif(isset($swaps['DEFAULT']))
				$result = str_replace($find, $replace, $swaps['DEFAULT']);
			if($result != 'SKIP' && $result != '')
				echo('<tr><th>'.$keys[$i].'</th><td>'.$result.'</td></tr>');
		}
	}
	else{				// PRINTS A HORIZONTAL TABLE IF THERE IS MORE THAN ONE ROW
		echo ('<tr>');
		for($i = 0; $i < count($keys); $i++)
		{
			$result = $row[$keys[$i]];
			if(isset($swaps[$keys[$i]]))
				$result = $swaps[$keys[$i]];
			if($result != 'SKIP' && $result != '' && !strchr($result, 'type="hidden"'))
				echo('<th>'.$keys[$i].'</th>');
		}
		echo ('</tr>');

		for($r = 0; $r < $nbRows; $r++){
			echo ('<tr>');
			for($i = 0; $i < count($keys); $i++){
				$result = $row[$keys[$i]];
				$find = array("KEYMARKER", "VALUEMARKER", "INITMARKER");
				$replace = array($keys[$i]."-$r", $row[$keys[$i]], $row[$keys[0]]);
				if(isset($swaps[$keys[$i]]))
					$result = str_replace($find, $replace, $swaps[$keys[$i]]);
				elseif(isset($swaps['DEFAULT']))
					$result = str_replace($find, $replace, $swaps['DEFAULT']);
				if($result != 'SKIP' && $result != '')
				{
					if(!strchr($result, 'type="hidden"')){
					echo('<td>'.$result.'</td>');}
					else echo $result;
				}

			}
			echo ('</tr>');
			if($r != $nbRows - 1)$row = mysql_fetch_array($table, MYSQL_ASSOC);
		}
	}
	echo ('</table>');
}}

?>
